
package com.rasenhund.android.paintstock;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

import com.rasenhund.android.paintstock.dao.StockDao;
import com.rasenhund.android.paintstock.schema.Stock;

/**
 * 在庫ありリスト
 */
public class StockListFragment extends Fragment implements EditDialogFragment.Callback {

    // パラメータ受け渡し用キー
    public static final String KEY_SECTION_NUMBER = "section_number";

    ListView mListView = null;
    StockListAdapter mAdapter = null;
    SearchView mSearchView = null;
    int miSectionNo = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // パラメータ取得
        miSectionNo = getArguments().getInt(KEY_SECTION_NUMBER);

        // Viewの初期設定
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);
        mListView = (ListView) rootView.findViewById(R.id.listView1);
        mListView.setOnItemClickListener(listItem_OnClickListener);
        mListView.setTextFilterEnabled(true);

        mSearchView = (SearchView) rootView.findViewById(R.id.searchView1);
        mSearchView.setOnQueryTextListener(searchView_OnQueryTextListener);
        mSearchView.setQueryHint("塗料名を入力");
        mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                
            }
        });

        return rootView;

    }

    @Override
    public void onResume() {
        super.onResume();

        // 初期データ取得
        getData();

    }

    /**
     * 登録完了時の処理
     */
    @Override
    public void onRegistCompleted(String tag, Bundle args, int which) {

        // フィルタは一旦解除
        mListView.clearTextFilter();

        // データ再取得
        getData();

    }

    /**
     * 表示用データ再取得
     */
    public void getData() {
        
        ArrayList<Stock> items = new ArrayList<Stock>();

        StockDao dao = new StockDao(getActivity().getApplicationContext());
        if (miSectionNo == 0) {
            // 在庫あり全件の取得
            items = dao.selectZaiko();
        } else {
            // 在庫なしを含む全件の取得
            items = dao.selectZaikoAriNashi();
        }
        
        mAdapter = new StockListAdapter(getActivity().getApplicationContext(),
                R.layout.row_stocklist, items);
        mListView.setAdapter(mAdapter);

        return;

    }

    /**
     * ListItem クリック時の処理
     */
    private OnItemClickListener listItem_OnClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            ListView listView = (ListView) parent;

            String sName = "";
            Object item = listView.getItemAtPosition(position);

            sName = ((Stock) item).getPaintName();

            // 登録用ダイアログ呼び出し
            FragmentManager manager = getActivity().getSupportFragmentManager();
            // コールバックメソッドを呼び出すため、引数ありコンストラクタで宣言
            EditDialogFragment dialogFragment = new EditDialogFragment(StockListFragment.this);
            // パラメータの設定
            Bundle args = new Bundle();
            // args.putInt(EditDialogFragment.KEY_PARAM_POSITION, position);
            args.putString(EditDialogFragment.KEY_PARAM_NAME, sName);
            dialogFragment.setArguments(args);
            dialogFragment.show(manager, "dialog");

        }

    };

    /**
     * SearchView テキスト入力時の処理
     */
    private OnQueryTextListener searchView_OnQueryTextListener = new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                mListView.clearTextFilter();
            } else {
                mListView.setFilterText(newText);
            }
            return true;
        }
    };

    // private View.OnFocusChangeListener searchView_OnFocusChangeListener = new
    // View.OnFocusChangeListener() {
    //
    // @Override
    // public void onFocusChange(View v, boolean hasFocus) {
    // mSearchView.collapseActionView()
    // }
    // };

}
